import Mock from 'mockjs';

const users = [
    { username: 'admin', password: 'admin', role: "admin" },
    { username: 'user', password: '123456', role: "user" }
];


Mock.mock('/api/login', 'post', (options) => {
    const { username, password } = JSON.parse(options.body);
    const user = users.find(u => u.username === username && u.password === password);
    if (user) {
        return {
            code: 200,
            data: {
                token: new Date().getTime(),
                username: user.username,
                role: user.role
            }
        };
    } else {
        return {
            code: 401,
            message: '用户名或密码错误'
        };
    }
});


Mock.mock('/api/registry', 'post', (options) => {
    const { username, password } = JSON.parse(options.body);
    const user = users.find(u => u.username === username && u.password === password);
    if (user) {
        return {
            code: 400,
            message: '该账户已存在'
        };
    } else {
        users.push({ username, password, role: "user" });
        return {
            code: 200,
            data: {
                username: username
            }
        };
    }
});

Mock.mock('/api/resetPwd', 'post', (options) => {
    const { username } = JSON.parse(options.body);
    const user = users.find(u => u.username === username);
    if (user) {
        user.password = '123456';
        return {
            code: 200,
            message: '密码重置成功'
        };
    } else {
        return {
            code: 401,
            message: '该账号不存在'
        };
    }
});